<template>
  <div class="auth-container">
    <div class="auth-background">
      <div class="background-overlay"></div>
      <div class="math-formulas">
        <div class="formula formula-1">π = 3.14159...</div>
        <div class="formula formula-2">E = mc²</div>
        <div class="formula formula-3">∫₀^∞ e^(-x²) dx</div>
        <div class="formula formula-4">a² + b² = c²</div>
        <div class="formula formula-5">∑ᵢ₌₁ⁿ i = n(n+1)/2</div>
        <div class="formula formula-6">f(x) = x² + 2x + 1</div>
        <div class="formula formula-7">lim[x→∞] (1 + 1/x)^x = e</div>
        <div class="formula formula-8">√(x² + y²)</div>
        <div class="formula formula-9">sin²θ + cos²θ = 1</div>
        <div class="formula formula-10">∇f = (∂f/∂x, ∂f/∂y, ∂f/∂z)</div>
        <div class="formula formula-11">α + β = γ</div>
        <div class="formula formula-12">∞</div>
        <div class="formula formula-13">x = (-b ± √(b²-4ac))/2a</div>
        <div class="formula formula-14">∂²u/∂t² = c²∇²u</div>
        <div class="formula formula-15">P(A∩B) = P(A)P(B|A)</div>
        
        <!-- 数学符号装饰 -->
        <div class="math-symbol symbol-1">π</div>
        <div class="math-symbol symbol-2">∑</div>
        <div class="math-symbol symbol-3">∫</div>
        <div class="math-symbol symbol-4">∞</div>
        <div class="math-symbol symbol-5">√</div>
        <div class="math-symbol symbol-6">α</div>
        <div class="math-symbol symbol-7">β</div>
        <div class="math-symbol symbol-8">θ</div>
        <div class="math-symbol symbol-9">Δ</div>
        <div class="math-symbol symbol-10">∇</div>
      </div>
    </div>
    
    <div class="auth-card">
      <div class="auth-header">
        <div class="logo-section">
          <div class="logo-icon">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
              <path d="M12 2C13.1 2 14 2.9 14 4C14 5.1 13.1 6 12 6C10.9 6 10 5.1 10 4C10 2.9 10.9 2 12 2ZM21 9V7L15 7.5V9M15 9.5V12.5C14.8 12.5 14.6 12.5 14.3 12.5L14.2 15L15 15L15.1 12.5C17.3 12.5 19 10.8 19 8.5V9M10 22C11.1 22 12 21.1 12 20C12 18.9 11.1 18 10 18C8.9 18 8 18.9 8 20C8 21.1 8.9 22 10 22Z"/>
            </svg>
          </div>
          <div class="logo-text">
            <h1>考研真题训练系统</h1>
            <p>欢迎使用</p>
          </div>
        </div>
      </div>

      <form @submit.prevent="handleLogin" class="auth-form">
        <div class="form-group">
          <label class="form-label">用户名</label>
          <div class="input-wrapper">
            <input 
              type="text" 
              v-model="form.username" 
              required 
              placeholder="请输入用户名"
              class="form-input"
            >
            <div class="input-icon">
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
                <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
              </svg>
            </div>
          </div>
        </div>

        <div class="form-group">
          <label class="form-label">密码</label>
          <div class="input-wrapper">
            <input 
              type="password" 
              v-model="form.password" 
              required 
              placeholder="请输入密码"
              class="form-input"
            >
            <div class="input-icon">
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
                <path d="M18,8A2,2 0 0,1 20,10V20A2,2 0 0,1 18,22H6A2,2 0 0,1 4,20V10A2,2 0 0,1 6,8H7V6A5,5 0 0,1 12,1A5,5 0 0,1 17,6V8H18M12,3A3,3 0 0,0 9,6V8H15V6A3,3 0 0,0 12,3Z"/>
              </svg>
            </div>
          </div>
        </div>

        <div class="form-options">
          <div class="forgot-password">
            <router-link to="/find-password">忘记密码？</router-link>
          </div>
        </div>

        <button type="submit" class="auth-button primary">
          <span>立即登录</span>
          <div class="button-overlay"></div>
        </button>

        <div v-if="errorMsg" class="error-message">
          <div class="error-icon">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
              <path d="M13,13H11V7H13M13,17H11V15H13M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z"/>
            </svg>
          </div>
          {{ errorMsg }}
        </div>
        
        <div class="register-guide" v-if="showRegisterGuide">
          <div class="guide-content">
            <div class="guide-icon">
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
                <path d="M13,9H11V7H13M13,17H11V11H13M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z"/>
              </svg>
            </div>
            <div class="guide-text">
              <p>该账号尚未注册</p>
              <router-link to="/register">立即注册新账号</router-link>
            </div>
          </div>
        </div>
      </form>

      <div class="auth-footer">
        <p>还没有账号？<router-link to="/register">立即注册</router-link></p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/store/user';

const form = ref({
  username: '',
  password: ''
});
const errorMsg = ref('');
const router = useRouter();
const userStore = useUserStore();

const showRegisterGuide = computed(() => {
  return errorMsg.value.includes('用户不存在');
});

const handleLogin = async () => {
  errorMsg.value = '';
  try {
    const response = await axios.post('/api/userlogin/login', form.value);
    if (response.data.code === 200) {
      userStore.setUser(response.data.data);
      if (response.data.data.role === 'student') {
        router.push('/home');
      } else {
        router.push('/admin/dashboard');
      }
    } else {
      if (response.data.code === 404) {
        errorMsg.value = '用户不存在';
      } else {
        errorMsg.value = response.data.msg;
      }
    }
  } catch (error) {
    if (error.response && error.response.status === 404) {
      errorMsg.value = '用户不存在';
    } else {
      errorMsg.value = '登录失败，请检查网络或输入内容';
    }
    console.error('登录请求失败:', error);
  }
};
</script>

<style scoped>
.auth-container {
  min-height: 100vh;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  overflow: hidden;
}

.auth-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  z-index: -2;
}

.background-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #4e6694 0%, #3b527a 50%, #1a2f54 100%);
  opacity: 0.85;
}

.math-formulas {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: 'Times New Roman', serif;
}

/* 数学公式样式 */
.formula {
  position: absolute;
  color: rgba(255, 255, 255, 0.4);
  font-size: 32px;
  font-weight: 300;
  white-space: nowrap;
  pointer-events: none;
  animation: float-formula 15s ease-in-out infinite;
}

.formula-1 {
  top: 10%;
  left: 5%;
  font-size: 30px;
  animation-delay: 0s;
}

.formula-2 {
  top: 20%;
  right: 10%;
  font-size: 34px;
  animation-delay: 2s;
}

.formula-3 {
  top: 35%;
  left: 15%;
  font-size: 28px;
  animation-delay: 4s;
}

.formula-4 {
  top: 15%;
  left: 40%;
  font-size: 32px;
  animation-delay: 6s;
}

.formula-5 {
  top: 45%;
  right: 20%;
  font-size: 30px;
  animation-delay: 8s;
}

.formula-6 {
  top: 60%;
  left: 8%;
  font-size: 31px;
  animation-delay: 10s;
}

.formula-7 {
  top: 25%;
  right: 35%;
  font-size: 29px;
  animation-delay: 12s;
}

.formula-8 {
  top: 70%;
  left: 35%;
  font-size: 33px;
  animation-delay: 14s;
}

.formula-9 {
  top: 55%;
  right: 5%;
  font-size: 30px;
  animation-delay: 1s;
}

.formula-10 {
  top: 80%;
  left: 60%;
  font-size: 28px;
  animation-delay: 3s;
}

.formula-11 {
  top: 5%;
  right: 50%;
  font-size: 32px;
  animation-delay: 5s;
}

.formula-12 {
  top: 40%;
  left: 70%;
  font-size: 38px;
  animation-delay: 7s;
}

.formula-13 {
  top: 85%;
  right: 15%;
  font-size: 27px;
  animation-delay: 9s;
}

.formula-14 {
  top: 30%;
  left: 25%;
  font-size: 29px;
  animation-delay: 11s;
}

.formula-15 {
  top: 75%;
  right: 40%;
  font-size: 30px;
  animation-delay: 13s;
}

/* 数学符号样式 */
.math-symbol {
  position: absolute;
  color: rgba(255, 255, 255, 0.35);
  font-size: 80px;
  font-weight: 300;
  pointer-events: none;
  animation: float-symbol 12s ease-in-out infinite;
}

.symbol-1 {
  top: 12%;
  right: 25%;
  animation-delay: 1.5s;
}

.symbol-2 {
  top: 65%;
  left: 20%;
  animation-delay: 3.5s;
}

.symbol-3 {
  top: 35%;
  right: 8%;
  animation-delay: 5.5s;
}

.symbol-4 {
  top: 18%;
  left: 75%;
  animation-delay: 7.5s;
}

.symbol-5 {
  top: 50%;
  left: 5%;
  animation-delay: 9.5s;
}

.symbol-6 {
  top: 78%;
  right: 65%;
  animation-delay: 11.5s;
}

.symbol-7 {
  top: 8%;
  left: 60%;
  animation-delay: 2.5s;
}

.symbol-8 {
  top: 42%;
  right: 45%;
  animation-delay: 4.5s;
}

.symbol-9 {
  top: 88%;
  left: 15%;
  animation-delay: 6.5s;
}

.symbol-10 {
  top: 28%;
  left: 85%;
  animation-delay: 8.5s;
}

@keyframes float-formula {
  0% { 
    transform: translateY(0) rotate(0deg);
    opacity: 0;
  }
  15% { 
    transform: translateY(-10px) rotate(1deg);
    opacity: 0.5;
  }
  30% { 
    transform: translateY(-15px) rotate(2deg);
    opacity: 0.6;
  }
  45% { 
    transform: translateY(-8px) rotate(-1deg);
    opacity: 0.4;
  }
  60% { 
    transform: translateY(-20px) rotate(1.5deg);
    opacity: 0.55;
  }
  75% { 
    transform: translateY(-12px) rotate(-0.5deg);
    opacity: 0.3;
  }
  90% { 
    transform: translateY(-5px) rotate(0.5deg);
    opacity: 0.1;
  }
  100% { 
    transform: translateY(0) rotate(0deg);
    opacity: 0;
  }
}

@keyframes float-symbol {
  0% { 
    transform: translateY(0) rotate(0deg) scale(1);
    opacity: 0;
  }
  20% { 
    transform: translateY(-15px) rotate(3deg) scale(1.05);
    opacity: 0.4;
  }
  40% { 
    transform: translateY(-25px) rotate(5deg) scale(1.1);
    opacity: 0.5;
  }
  60% { 
    transform: translateY(-10px) rotate(-3deg) scale(0.95);
    opacity: 0.35;
  }
  80% { 
    transform: translateY(-18px) rotate(2deg) scale(1.02);
    opacity: 0.2;
  }
  95% { 
    transform: translateY(-5px) rotate(-1deg) scale(0.98);
    opacity: 0.05;
  }
  100% { 
    transform: translateY(0) rotate(0deg) scale(1);
    opacity: 0;
  }
}

.auth-card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 420px;
  overflow: hidden;
  animation: slideInUp 0.6s ease-out;
  position: relative;
  backdrop-filter: blur(10px);
  z-index: 10;
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.auth-header {
  background: linear-gradient(135deg, #4e6694 0%, #3b527a 100%);
  padding: 30px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.auth-header::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
  animation: rotate 20s linear infinite;
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.logo-section {
  position: relative;
  z-index: 1;
}

.logo-icon {
  width: 60px;
  height: 60px;
  margin: 0 auto 15px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(10px);
}

.logo-icon svg {
  width: 30px;
  height: 30px;
  color: white;
}

.logo-text h1 {
  color: white;
  margin: 0 0 5px 0;
  font-size: 24px;
  font-weight: 600;
}

.logo-text p {
  color: rgba(255, 255, 255, 0.8);
  margin: 0;
  font-size: 14px;
}

.auth-form {
  padding: 30px;
  position: relative;
  z-index: 5;
}

.form-group {
  margin-bottom: 24px;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: #1a2f54;
  font-size: 14px;
}

.input-wrapper {
  position: relative;
  z-index: 1;
}

.form-input {
  width: 100%;
  padding: 14px 16px 14px 50px;
  border: 2px solid #e6e9ed;
  border-radius: 10px;
  font-size: 15px;
  transition: all 0.3s ease;
  background: #f8f9fa;
  position: relative;
  z-index: 2;
  box-sizing: border-box;
}

.form-input:focus {
  border-color: #4e6694;
  background: white;
  box-shadow: 0 0 0 3px rgba(78, 102, 148, 0.1);
  outline: none;
}

.input-icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #9ca3af;
  transition: color 0.3s ease;
  z-index: 3;
  pointer-events: none;
}

.form-input:focus + .input-icon {
  color: #4e6694;
}

.input-icon svg {
  width: 20px;
  height: 20px;
}

.form-options {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 24px;
}

.forgot-password a {
  color: #4e6694;
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  transition: color 0.3s ease;
}

.forgot-password a:hover {
  color: #3b527a;
}

.auth-button {
  width: 100%;
  padding: 16px;
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
  margin-bottom: 20px;
}

.auth-button.primary {
  background: linear-gradient(135deg, #4e6694 0%, #3b527a 100%);
  color: white;
  box-shadow: 0 4px 15px rgba(78, 102, 148, 0.3);
}

.auth-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(78, 102, 148, 0.4);
}

.button-overlay {
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
  transition: left 0.5s ease;
}

.auth-button:hover .button-overlay {
  left: 100%;
}

.error-message {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: linear-gradient(135deg, #fee2e2, #fecaca);
  border: 1px solid #fca5a5;
  border-radius: 8px;
  color: #dc2626;
  font-size: 14px;
  margin-bottom: 16px;
}

.error-icon svg {
  width: 16px;
  height: 16px;
}

.register-guide {
  background: linear-gradient(135deg, #fef3c7, #fde68a);
  border: 1px solid #f59e0b;
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 16px;
}

.guide-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.guide-icon {
  color: #d97706;
}

.guide-icon svg {
  width: 20px;
  height: 20px;
}

.guide-text p {
  margin: 0 0 4px 0;
  color: #92400e;
  font-weight: 500;
}

.guide-text a {
  color: #d97706;
  text-decoration: none;
  font-weight: 600;
}

.guide-text a:hover {
  text-decoration: underline;
}

.auth-footer {
  text-align: center;
  padding: 20px 30px 30px;
  color: #6b7280;
  font-size: 14px;
}

.auth-footer a {
  color: #4e6694;
  text-decoration: none;
  font-weight: 600;
  margin-left: 4px;
}

.auth-footer a:hover {
  text-decoration: underline;
}

@media (max-width: 480px) {
  .auth-container {
    padding: 16px;
  }
  
  .auth-card {
    max-width: 100%;
  }
  
  .auth-header {
    padding: 24px;
  }
  
  .auth-form {
    padding: 24px;
  }
  
  .logo-text h1 {
    font-size: 20px;
  }
  
  .formula {
    font-size: 26px;
  }
  
  .math-symbol {
    font-size: 64px;
  }
}

@media (max-width: 768px) {
  .formula {
    font-size: 28px;
  }
  
  .math-symbol {
    font-size: 72px;
  }
}
</style>